package com.zhonghui.kf.controller;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.zhonghui.common.core.domain.entity.SysUser;
import com.zhonghui.framework.websocket.MessageController;
import com.zhonghui.kf.domain.ProcurementChatShistory;
import com.zhonghui.kf.service.IProcurementChatShistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhonghui.common.core.controller.BaseController;
import com.zhonghui.common.core.domain.AjaxResult;
import com.zhonghui.kf.domain.ProcurementAnswer;
import com.zhonghui.kf.service.IProcurementAnswerService;
import com.zhonghui.common.core.page.TableDataInfo;

/**
 * 客服问答Controller
 *
 * @author y
 * @date 2023-11-29
 */
@RestController
@RequestMapping("/chat/pcAnswer")
public class ProcurementAnswerPcController extends BaseController
{
    @Autowired
    private IProcurementAnswerService procurementAnswerService;
    @Autowired
    private IProcurementChatShistoryService procurementChatShistoryService;
    /**
     * 查询客服问答列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProcurementAnswer procurementAnswer)
    {
        startPage();
        List<ProcurementAnswer> list = procurementAnswerService.selectProcurementAnswerList(procurementAnswer);
        return getDataTable(list);
    }


    /**
     * 获取客服问答详细信息
     */
    @GetMapping(value = "/getAnswer")
    public AjaxResult getInfo(ProcurementAnswer procurementAnswer)
    {
        ProcurementAnswer prc =  procurementAnswerService.selectProcurementAnswer(procurementAnswer);
        if(ObjectUtil.isEmpty(prc)){
            prc = new ProcurementAnswer();
            prc.setAnswer("抱歉，您的问题暂时无法回复，请点击【转人工】按钮，进行转接");
        }
// 用户
        if(procurementAnswer.getType()==1){
        ProcurementChatShistory procurementChatShistory = new ProcurementChatShistory();
        procurementChatShistory.setContext(procurementAnswer.getAsk());
        procurementChatShistory.setUserId(procurementAnswer.getUserId());
        procurementChatShistory.setType(1);
        procurementChatShistory.setTime(procurementAnswer.getTime());
        procurementChatShistoryService.insertProcurementChatShistory(procurementChatShistory);


            ProcurementChatShistory procurementChatShistory1 = new ProcurementChatShistory();
            procurementChatShistory1.setContext(prc.getAnswer());
            procurementChatShistory1.setUserId((long) 0);
            procurementChatShistory1.setType(0);
            procurementChatShistory1.setTime(new Date());
            procurementChatShistoryService.insertProcurementChatShistory(procurementChatShistory1);
            return success(prc);
        }
//客服

        if(procurementAnswer.getType()==2) {
            ProcurementChatShistory procurementChatShistory = new ProcurementChatShistory();
            procurementChatShistory.setContext(procurementAnswer.getAsk());
            SysUser sysUser= procurementChatShistoryService.getUserChat();
            MessageController messageController = new MessageController();
//0 客服 1用户 UserType
            procurementChatShistory.setUserId(procurementAnswer.getUserId());
            if(procurementAnswer.getUserType()==1){
                procurementChatShistory.setPid(sysUser.getUserId());
                procurementChatShistory.setType(1);
                messageController.sendOneMessage(String.valueOf(sysUser.getUserId())," {\"id\":"+procurementAnswer.getUserId()+",\"msgType\": 1,\"type\":"+procurementChatShistory.getType()+",\"context\":"+procurementChatShistory.getContext()+"}");
            }else{
                procurementChatShistory.setPid(procurementAnswer.getPid());
                procurementChatShistory.setType(0);
                messageController.sendOneMessage(String.valueOf(procurementAnswer.getUserId())," {\"id\":"+procurementAnswer.getUserId()+",\"msgType\": 1,\"type\":"+procurementChatShistory.getType()+",\"context\":"+procurementChatShistory.getContext()+"}");
            }
            procurementChatShistory.setTime(procurementAnswer.getTime());
            procurementChatShistoryService.insertProcurementChatShistory(procurementChatShistory);

            //msgType 消息类型 1通知

//            messageController.sendOneMessage("110","{\"msgType\":0,\"data\":"+sessionPool.keySet()+"}"  );



//            ProcurementChatShistory procurementChatShistory1 = new ProcurementChatShistory();
//            procurementChatShistory1.setContext(prc.getAnswer());
//            procurementChatShistory1.setUserId((long) 0);
//            procurementChatShistory1.setType(0);
//            procurementChatShistory1.setTime(new Date());
//            procurementChatShistoryService.insertProcurementChatShistory(procurementChatShistory1);
            return success(null);
        }
        return success(null);
    }


}
